package jenry.xml;

import java.util.ArrayList;
import java.util.List;

import jenry.model.Table_v;
import jenry.utils.toString;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TableListContentHandler extends DefaultHandler {
	private List<Table_v> infos = null;
	private Table_v TableInfo = null;
	private String tagName =null;
	private ArrayList<String> temp_td = null;
	
	public TableListContentHandler(List<Table_v> infos) {
		super();
		this.infos = infos;
	}

	public List<Table_v> getInfos() {
		return infos;
	}

	public void setInfos(List<Table_v> infos) {
		this.infos = infos;
	}

	public void startDocument() throws SAXException {
		System.out.println("--------startDocument-------");
		super.startDocument();
	}
	
	public void endDocument() throws SAXException {
		System.out.println("--------endDocument-------");
		//super.endDocument();
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		System.out.println("--------startElement-------tagName:"+tagName+"--localName:"+localName);
		tagName = localName;
		if(tagName.equals("tr")){
			TableInfo = new Table_v();
			temp_td = new ArrayList<String>();
		}
	}
	
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String temp = new String(ch, start, length);
		temp=toString.getStringNoBlank(temp);
		System.out.println("--------characters-------tagName:"+tagName+"--temp:"+temp);
		if (tagName.equals("tr"))
			TableInfo.setTr(temp);
		else if (tagName.equals("td"))
			temp_td.add(temp);
		System.out.println("endCharacters");
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		System.out.println("--------endElement-------qName:"+qName+"--localName:"+localName);
		if(localName.equals("tr")){
			TableInfo.setTd(temp_td);
			infos.add(TableInfo);
		}
		//temp_td.clear();
		tagName="";
	}
}
